﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Reflection;
using eCommerce.db.DataRespository;
using eCommerce.db.SQLDataAccess;

namespace eCommerce.db.DALRespository
{
    public class IeDetailDal
    {
        #region IeDetailDal private
        private static void SetInfo(IDataRecord reader, ref IeDetailEntity o)
        {
            for (int i = 0; i < reader.FieldCount; i++)
            {
                PropertyInfo propertyInfo = DbCommon.GetPropertyInfo(o, reader.GetName(i));
                if (((propertyInfo != null) && (reader[i] != null)) && (reader[i] != DBNull.Value))
                    propertyInfo.SetValue(o, reader[i], null);
                else
                    o[reader.GetName(i)] = reader[i];
            }
        }

        private static void SetInfoList(ref SqlDataReader reader, ref List<IeDetailEntity> productList)
        {
            while (reader.Read())
            {
                var o = new IeDetailEntity();
                SetInfo(reader, ref o);
                productList.Add(o);
            }
            reader.Dispose();
        }
        #endregion

        public static List<IeDetailEntity> ListDistinctProductByIeId(long ieId)
        {                       
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstProduct = new List<IeDetailEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString,
                    DbCommon.DatabaseSchema + "IeDetail_GetDistinctProductByIeId", ieId);
                SetInfoList(ref reader, ref lstProduct);
                return lstProduct;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "IeDetail_GetDistinctProductByIeId: {0}", ex.Message));
            }            
        }

        public static List<IeDetailEntity> ListSizeAndQuantityByProduct(long ieId, int productId)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            var lstProduct = new List<IeDetailEntity>();
            try
            {
                var reader = SqlHelper.ExecuteReader(DbCommon.ConnectionString,
                    DbCommon.DatabaseSchema + "IeDetail_GetSizeAndQuantityByProduct", ieId, productId);
                SetInfoList(ref reader, ref lstProduct);
                return lstProduct;
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "IeDetail_GetSizeAndQuantityByProduct: {0}", ex.Message));
            }                
        }

        public static void Create(IeDetailEntity o)
        {
            DbConnection.SetConnection(DbConnection.ConnectionName.Main);
            try
            {
                SqlHelper.ExecuteNonQuery(
                    DbCommon.ConnectionString,
                    DbCommon.DatabaseSchema + "IeDetail_Create", o.IeId, o.SizeId, o.ParentId, o.CategoryId, o.ProductId, o.Quantity);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "IeDetail_Create: {0}", ex.Message));
            }
        }

        public static void Delete(long ieId, int productId)
        {
            try
            {
                if (ieId > 0 && productId > 0)
                    SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "IeDetail_Delete", ieId, productId);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "IeDetail_Delete: {0}", ex.Message));
            }
        }

        public static void DeleteByIeId(int ieId)
        {
            try
            {
                if (ieId > 0)
                    SqlHelper.ExecuteNonQuery(DbCommon.ConnectionString, DbCommon.DatabaseSchema + "IeDetail_Delete_ByIeId", ieId);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format(DbCommon.DatabaseSchema + "IeDetail_Delete_ByIeId: {0}", ex.Message));
            }
        }
    }
}
